﻿using Architecture.Domain.Services.Interface;
using Architecture.Domian.Common;
using Architecture.Domian.DTO;
using Architecture.Domian.Entity;
using Architecture.Repository;
using Architecture.Repository.Interface;
using Microsoft.VisualBasic;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Architecture.Domain.Services.Impl
{
    public class SourceService : ISourceService
    {
        private readonly IRepository<GoodsInfo> _goodRepository;
        private readonly ISourceRepository _sourceRepository;

        public SourceService(IRepository<GoodsInfo> goodRepository, ISourceRepository sourceRepository)
        {
            _goodRepository = goodRepository;
            _sourceRepository = sourceRepository;
        }

        public async Task<List<GoodsInfo>> GetGoodsInfoAsync()
        {
            return await _goodRepository.GetAllAsync();
        }

        public async Task<int> SourceInsert(SourceInfo source)
        {
            var sourceInfo = await _sourceRepository.GetByFuncAsync(x => !x.IsDelete && x.ProductionBatch.Equals(source.ProductionBatch));
            if (sourceInfo != null) return -1; // 该商品溯源信息存在
            source.CreateBy = "张三";
            source.CreateDate = DateTime.Now;
            source.UpdateBy = null;
            source.UpdateDate = null;
            return await _sourceRepository.AddAsync(source);
        }

        public async Task<PageResult<SourceInfoDTO>> GetSourceInfoPage(long goodNo, string? batch, int deadline, int size, int index)
        {
            var sourceInfo = await _sourceRepository.GetAsync(x => !x.IsDelete);
            var list = new List<SourceInfoDTO>();
            foreach (var item in sourceInfo)
            {
                var goodInfo = await _goodRepository.GetByFuncAsync(x => x.GoodNo.Equals(item.GoodNo));
                var sourceDTO = new SourceInfoDTO
                {
                    SourceId = item.SourceId,
                    SourceNo = item.SourceNo,
                    GoodNo = item.GoodNo,
                    ShelfLife = item.ShelfLife,
                    CreateBy = item.CreateBy,
                    CreateDate = item.CreateDate,
                    UpdateBy = item.UpdateBy,
                    UpdateDate = item.UpdateDate,
                    ProductionBatch = item.ProductionBatch,
                    ProductionEnterprise = item.ProductionEnterprise,
                    ProductionLicenseNumber = item.ProductionLicenseNumber,
                    ProductInspectionReport = item.ProductInspectionReport,
                    IsDelete = item.IsDelete,
                    GoodName = goodInfo.GoodName,
                };
                list.Add(sourceDTO);
            }


            var listAQ = list.AsQueryable();
            if (goodNo != 0)
            {
                listAQ = listAQ.Where(x => x.GoodNo.Equals(goodNo));
            }
            if (!string.IsNullOrEmpty(batch))
            {
                listAQ = listAQ.Where(x => x.ProductionBatch.Equals(batch));
            }
            if (deadline != 0)
            {
                listAQ = listAQ.Where(x => x.ShelfLife.Equals(deadline));
            }

            int totalCount = sourceInfo.Count();
            int pageCount = (int)Math.Ceiling(totalCount * 1.0 / size);

            listAQ = listAQ.OrderByDescending(x => x.SourceId).Skip(size * (index - 1)).Take(size);

            return new PageResult<SourceInfoDTO>
            {
                Datas = listAQ.ToList(),
                TotalCount = totalCount,
                PageCount = pageCount,
            };
        }

        public async Task<SourceInfoDTO> GetSourceInfoBySourceId(int sourceId)
        {
            var sourceInfo = await _sourceRepository.GetByIdAsync(sourceId);
            var sourceDTO = new SourceInfoDTO
            {
                SourceId = sourceId,
                CreateBy = sourceInfo.CreateBy,
                CreateDate = sourceInfo.CreateDate,
                GoodNo = sourceInfo.GoodNo,
                IsDelete = sourceInfo.IsDelete,
                ProductionBatch = sourceInfo.ProductionBatch,
                ProductionEnterprise = sourceInfo.ProductionEnterprise,
                ProductionLicenseNumber = sourceInfo.ProductionLicenseNumber,
                ProductInspectionReport = sourceInfo.ProductInspectionReport,
                ShelfLife = sourceInfo.ShelfLife,
                SourceNo = sourceInfo.SourceNo,
                UpdateBy = sourceInfo.UpdateBy,
                UpdateDate = sourceInfo.UpdateDate,
                GoodPicture = sourceInfo.GoodPicture,
                GoodName = _goodRepository.GetByFuncAsync(x => x.GoodNo.Equals(sourceInfo.GoodNo)).Result.GoodName,
            };
            return sourceDTO;
        }

        public async Task<int> SourceUpdate(SourceInfo source)
        {
            var sourceInfo = await _sourceRepository.GetByFuncAsync(x => !x.IsDelete && x.SourceId != source.SourceId && x.ProductionBatch.Equals(source.ProductionBatch));
            if (sourceInfo != null) return -1; // 该商品溯源批次号存在
            source.UpdateBy = "李四";
            source.UpdateDate = DateTime.Now;
            return await _sourceRepository.UpdateAsync(source);
        }

        public async Task<int> SourceLogicDelete(int sourceId)
        {
            var sourceInfo = await _sourceRepository.GetByIdAsync(sourceId);
            sourceInfo.IsDelete = true;
            return await _sourceRepository.UpdateAsync(sourceInfo);
        }

        public async Task<int> SourceBacthLogicDelete(string? sourceIds)
        {
            var res = 0;
            var sourceIdsList = sourceIds.Split(',');
            foreach (string sourceId in sourceIdsList)
            {
                var source = await _sourceRepository.GetByIdAsync(Convert.ToInt32(sourceId));
                source.IsDelete = true;
                res += await _sourceRepository.UpdateAsync(source);
            }
            return res;
        }

        public async Task<List<PictureDTO>> GetPicture(string production)
        {
            var picture = await _sourceRepository.GetByFuncAsync(x => x.ProductionBatch == production && !x.IsDelete);

            if (string.IsNullOrEmpty(picture.ProductInspectionReport))
            {
                return new List<PictureDTO>();
            }

            var picList = picture.ProductInspectionReport.Split(",").Select(x => new PictureDTO
            {
                name = x,
                url = "http://localhost:5124/Images/" + x
            }).ToList();

            return picList;
        }

        public async Task<int> BatchPic(string production, string? imgUrls)
        {
            var source = await _sourceRepository.GetByFuncAsync(x => x.ProductionBatch == production && !x.IsDelete);

            source.ProductInspectionReport = imgUrls;

            return await _sourceRepository.UpdateAsync(source);
        }
    }
}
